package com.onepower.admin.controller;


import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.onepower.core.domain.request.MatchAdminForm;
import com.onepower.core.domain.request.MatchScoreRuleProjectForm;
import com.onepower.core.domain.response.RefereeInfo;
import com.onepower.core.modules.repository.entity.*;
import com.onepower.core.modules.service.*;
import com.onepower.core.result.CommonResult;
import com.onepower.core.utils.JsonUtil;
import com.onepower.core.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;

@RestController
@RequestMapping("/api/match/score/rule")
public class MatchScoreRuleController {

    @Autowired
    private IMatchScoreRuleService matchScoreRuleService;

    @Autowired
    private IMatchScoreRuleProjectService matchScoreRuleProjectService;

    @Autowired
    private IMatchScoreRuleCalculateService matchScoreRuleCalculateService;

    @Autowired
    private IMatchScoreRuleBaseRateService matchScoreRuleBaseRateService;

    @Autowired
    private IMatchScoreRuleIntervalScoreService matchScoreRuleIntervalScoreService;

    @Autowired
    private IMatchScoreBaseArithmeticRuleService matchScoreBaseArithmeticRuleService;

    @Autowired
    private IMatchScoreArithmeticRuleService matchScoreArithmeticRuleService;

    @Autowired
    private IMatchScoreBaseOrderRuleService matchScoreBaseOrderRuleService;

    @Autowired
    private IMatchScoreOrderRuleService matchScoreOrderRuleService;

    @Autowired
    private IMatchScoreStatusRuleService matchScoreStatusRuleService;

    @Autowired
    private IMatchScoreIpadArithmeticService matchScoreIpadArithmeticService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list() {
        List<MatchScoreRule> list = matchScoreRuleService.scoreRuleList();
        return ResultUtils.ok(list);
    }

    @RequestMapping(value = "save", method = RequestMethod.GET)
    public CommonResult save(@RequestParam(value = "name") String name) {
        MatchScoreRule matchScoreRule = matchScoreRuleService.getScoreRuleByName(name);
        if (matchScoreRule == null || matchScoreRule.getIsDelete() == 1){
            matchScoreRuleService.save(name);
            return ResultUtils.ok();
        }else {
            return ResultUtils.error(500,"规则名已存在。");
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public CommonResult update(@RequestParam(value = "name") String name, @RequestParam(value = "id") Integer id) {
        MatchScoreRule matchScoreRule = matchScoreRuleService.getScoreRuleByName(name);
        if (matchScoreRule == null  || matchScoreRule.getIsDelete() == 1){
            matchScoreRuleService.updateScoreRule(id,name);
            return ResultUtils.ok();
        }else {
            return ResultUtils.error(500,"规则名已存在。");
        }
    }

    @RequestMapping(value = "ruleProjectList", method = RequestMethod.GET)
    public CommonResult ruleProjectList(@RequestParam(value = "ruleId", required = false) Integer ruleId) {
        List<MatchScoreRuleProject> list = matchScoreRuleProjectService.projectList(ruleId);
        for (int i=list.size();i<10;i++){
            MatchScoreRuleProject project = new MatchScoreRuleProject();
            list.add(project);
        }
        return ResultUtils.ok(list);
    }

    @RequestMapping(value = "ruleProjectListClient", method = RequestMethod.GET)
    public CommonResult ruleProjectListClient(@RequestParam(value = "ruleId", required = false) Integer ruleId) {
        List<MatchScoreRuleProject> list = matchScoreRuleProjectService.projectList(ruleId);
        return ResultUtils.ok(list);
    }

    @RequestMapping(value = "ruleProjectMapList", method = RequestMethod.GET)
    public CommonResult ruleProjectMapList(@RequestParam(value = "ruleId", required = false) Integer ruleId) {
        List<Map<String, Object>>  list = matchScoreRuleProjectService.projectMapList(ruleId);
        for (int i=list.size();i<10;i++){
            Map<String, Object> map = new HashMap<>();
            list.add(map);
        }
        return ResultUtils.ok(list);
    }

    @RequestMapping(value = "saveProject", method = RequestMethod.POST)
    public CommonResult saveProject(@RequestParam(value = "list", required = false) String list, @RequestParam(value = "calculateList", required = false) String calculateList)  throws IOException {
        String formStr = URLDecoder.decode(list,"UTF-8");
        List<JSONObject> formList = JsonUtil.getInstance().json2Object(formStr,List.class);
        String calculateListStr = URLDecoder.decode(calculateList,"UTF-8");
        List<JSONObject> formCalculateList = JsonUtil.getInstance().json2Object(calculateListStr,List.class);
        for (JSONObject projectObj : formList){
            MatchScoreRuleProject project = new MatchScoreRuleProject();
            project.setId((Integer) projectObj.get("id"));
            project.setRuleId((Integer) projectObj.get("ruleId"));
            project.setBelong((Integer) projectObj.get("belong"));
            project.setChildName((String)projectObj.get("childName"));
            project.setChildTop(Float.valueOf(projectObj.get("childTop")+""));
            if (projectObj.get("id") == null){
                project.setCreateTime(new Date());
            }
            matchScoreRuleProjectService.saveOrUpdate(project);
        }
        for (JSONObject calculateObj : formCalculateList){
            /*MatchScoreRuleCalculate calculate = new MatchScoreRuleCalculate();
            calculate.setScoreOption(calculateObj.getInteger("scoreOption"));
            calculate.setScoreCalculate(calculateObj.getInteger("scoreCalculate"));
            calculate.setType(calculateObj.getInteger("type"));
            calculate.setId((Integer) calculateObj.get("id"));
            calculate.setRuleId(calculateObj.getInteger("ruleId"));
            if (calculate.getId() == null){
                calculate.setCreateTime(new Date());
            }
            matchScoreRuleCalculateService.saveOrUpdate(calculate);*/

            MatchScoreIpadArithmetic m = new MatchScoreIpadArithmetic();
            m.setId((Integer) calculateObj.get("id"));
            m.setArithmeticRule((Integer) calculateObj.get("arithmeticRule"));
            m.setSelectRule((Integer) calculateObj.get("selectRule"));
            m.setName((String)calculateObj.get("name"));
            matchScoreIpadArithmeticService.updateById(m);
        }
        return ResultUtils.ok();
    }


    @RequestMapping(value = "ruleScoreCalculateList", method = RequestMethod.GET)
    public CommonResult ruleScoreCalculateList(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        List<MatchScoreRuleCalculate> list = matchScoreRuleCalculateService.calculateList(ruleId);
        if (list.size() == 0){
            //精准度
            MatchScoreRuleCalculate calculate1 = new MatchScoreRuleCalculate();
            calculate1.setType(1);
          //  calculate1.setScoreCalculate(1);
           // calculate1.setScoreOption(1);
            list.add(calculate1);

            //表现力
            MatchScoreRuleCalculate calculate2 = new MatchScoreRuleCalculate();
            calculate2.setType(2);
         //   calculate2.setScoreCalculate(2);
         //   calculate2.setScoreOption(2);
            list.add(calculate2);
        }
        return ResultUtils.ok(list);
    }


    @RequestMapping(value = "ruleScoreBaseRateList", method = RequestMethod.GET)
    public CommonResult ruleScoreBaseRateList(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        List<MatchScoreRuleBaseRate> list = matchScoreRuleBaseRateService.baseRateList(ruleId);
        if (list.size() == 0){
            //精准度
            MatchScoreRuleBaseRate baseRate = new MatchScoreRuleBaseRate();
            baseRate.setRuleId(ruleId);
            baseRate.setBaseScore(Float.valueOf("0.1") );
            list.add(baseRate);
            //表现力
            MatchScoreRuleBaseRate baseRate2 = new MatchScoreRuleBaseRate();
            baseRate2.setRuleId(ruleId);
            baseRate2.setBaseScore(Float.valueOf("0.1") );
            list.add(baseRate2);
        }
        return ResultUtils.ok(list);
    }

    @RequestMapping(value = "ruleScoreIntervalScore", method = RequestMethod.GET)
    public CommonResult ruleScoreIntervalScore(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        MatchScoreRuleIntervalScore matchScoreRuleIntervalScore = matchScoreRuleIntervalScoreService.intervalScore(ruleId);
        if (matchScoreRuleIntervalScore == null){
            matchScoreRuleIntervalScore = new MatchScoreRuleIntervalScore();
            matchScoreRuleIntervalScore.setRuleId(ruleId);
        }
        return ResultUtils.ok(matchScoreRuleIntervalScore);
    }


    @RequestMapping(value = "saveScoreBaseRate", method = RequestMethod.POST)
    public CommonResult saveScoreBaseRate(@RequestParam(value = "ruleBaseRateList", required = false) String ruleBaseRateList, @RequestParam(value = "ruleScoreIntervalScore", required = false) String ruleScoreIntervalScore)  throws IOException {
        String formStr = URLDecoder.decode(ruleBaseRateList,"UTF-8");
        List<JSONObject> formList = JsonUtil.getInstance().json2Object(formStr,List.class);
        String ruleScoreIntervalScoreStr = URLDecoder.decode(ruleScoreIntervalScore,"UTF-8");
        MatchScoreRuleIntervalScore matchScoreRuleIntervalScore = JsonUtil.getInstance().json2Object(ruleScoreIntervalScoreStr,MatchScoreRuleIntervalScore.class);
        for (JSONObject projectObj : formList){
            MatchScoreRuleBaseRate baseRate = new MatchScoreRuleBaseRate();
            baseRate.setId((Integer) projectObj.get("id"));
            baseRate.setRuleId((Integer) projectObj.get("ruleId"));
            baseRate.setBaseScore(projectObj.getFloatValue("baseScore"));
            baseRate.setBaseRate(projectObj.getFloatValue("baseRate"));
            if (projectObj.get("id") == null){
                baseRate.setCreateTime(new Date());
            }
            matchScoreRuleBaseRateService.saveOrUpdate(baseRate);
        }
        if (matchScoreRuleIntervalScore.getId() == null){
            matchScoreRuleIntervalScore.setCreateTime(new Date());
        }
        matchScoreRuleIntervalScoreService.saveOrUpdate(matchScoreRuleIntervalScore);
        return ResultUtils.ok();
    }


    @RequestMapping(value = "baseArithmeticRuleList", method = RequestMethod.GET)
    public CommonResult baseArithmeticRuleList(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        List<MatchScoreBaseArithmeticRule> list = matchScoreBaseArithmeticRuleService.queryList();
        MatchScoreArithmeticRule matchScoreArithmeticRule = matchScoreArithmeticRuleService.arithmeticRule(ruleId);
        List<Map<String,Object>> dataList = new ArrayList<>();
        for (MatchScoreBaseArithmeticRule matchScoreBaseArithmeticRule : list){
            Integer id = matchScoreBaseArithmeticRule.getId();
            Map<String,Object> map = new HashMap<>();
            map.put("id",matchScoreBaseArithmeticRule.getId());
            map.put("ruleText",matchScoreBaseArithmeticRule.getRuleText());
            if (matchScoreArithmeticRule != null && id == matchScoreArithmeticRule.getArithmeticRuleId()){
                map.put("checked",1);
            }else {
                map.put("checked",0);
            }
            dataList.add(map);
        }
        return ResultUtils.ok(dataList);
    }

    @RequestMapping(value = "matchScoreArithmeticRule", method = RequestMethod.GET)
    public CommonResult matchScoreArithmeticRule(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        MatchScoreArithmeticRule matchScoreArithmeticRule = matchScoreArithmeticRuleService.arithmeticRule(ruleId);
        return ResultUtils.ok(matchScoreArithmeticRule);
    }

    @PostMapping(value = "saveMatchScoreArithmeticRule")
    public CommonResult saveMatchScoreArithmeticRule(@RequestParam(value = "id", required = false) Integer id,
                                                     @RequestParam(value = "ruleId", required = false) Integer ruleId,
                                                     @RequestParam(value = "arithmeticRuleId", required = false) Integer arithmeticRuleId) {
        MatchScoreArithmeticRule matchScoreArithmeticRule = new MatchScoreArithmeticRule();
        matchScoreArithmeticRule.setId(id);
        matchScoreArithmeticRule.setRuleId(ruleId);
        matchScoreArithmeticRule.setArithmeticRuleId(arithmeticRuleId);
                matchScoreArithmeticRuleService.saveOrUpdate(matchScoreArithmeticRule);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "baseOrderRuleList", method = RequestMethod.GET)
    public CommonResult baseOrderRuleList(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        List<MatchScoreBaseOrderRule> list = matchScoreBaseOrderRuleService.queryList();
        MatchScoreOrderRule matchScoreOrderRule = matchScoreOrderRuleService.orderRule(ruleId);

        List<Map<String,Object>> dataList = new ArrayList<>();
        for (MatchScoreBaseOrderRule matchScoreBaseOrderRule : list){
            Integer id = matchScoreBaseOrderRule.getId();
            Map<String,Object> map = new HashMap<>();
            map.put("id",matchScoreBaseOrderRule.getId());
            map.put("orderText",matchScoreBaseOrderRule.getOrderText());
            if (matchScoreOrderRule != null && id == matchScoreOrderRule.getOrderRuleId()){
                map.put("checked",1);
            }else {
                map.put("checked",0);
            }
            dataList.add(map);
        }
        return ResultUtils.ok(dataList);
    }

    @RequestMapping(value = "matchScoreOrderRule", method = RequestMethod.GET)
    public CommonResult matchScoreOrderRule(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        MatchScoreOrderRule matchScoreOrderRule = matchScoreOrderRuleService.orderRule(ruleId);
        return ResultUtils.ok(matchScoreOrderRule);
    }

    @PostMapping(value = "saveMatchScoreOrderRule")
    public CommonResult saveMatchScoreOrderRule(@RequestParam(value = "id", required = false) Integer id,
                                                     @RequestParam(value = "ruleId", required = false) Integer ruleId,
                                                     @RequestParam(value = "orderRuleId", required = false) Integer orderRuleId) {
        MatchScoreOrderRule matchScoreOrderRule = new MatchScoreOrderRule();
        matchScoreOrderRule.setId(id);
        matchScoreOrderRule.setRuleId(ruleId);
        matchScoreOrderRule.setOrderRuleId(orderRuleId);
        matchScoreOrderRuleService.saveOrUpdate(matchScoreOrderRule);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "matchScoreStatusRule", method = RequestMethod.GET)
    public CommonResult matchScoreStatusRule(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        MatchScoreStatusRule matchScoreStatusRule = matchScoreStatusRuleService.getScoreStatusRule(ruleId);
        return ResultUtils.ok(matchScoreStatusRule);
    }

    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.GET)
    public CommonResult saveOrUpdate(@RequestParam(value = "ruleId", required = false) Integer ruleId,
                                     @RequestParam(value = "status", required = false) Integer status){
        matchScoreStatusRuleService.saveOrUpdate(ruleId,status);
        return ResultUtils.ok();
    }

    @RequestMapping(value = "selectIpadList", method = RequestMethod.GET)
    public CommonResult selectIpadList(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        List<MatchScoreIpadArithmetic> list = matchScoreIpadArithmeticService.selectIpadList(ruleId);
        return ResultUtils.ok(list);
    }

    @RequestMapping(value = "deleteRuleById", method = RequestMethod.GET)
    public CommonResult deleteRuleById(@RequestParam(value = "ruleId", required = false) Integer ruleId){
        MatchScoreRule matchScoreRule = new MatchScoreRule();
        matchScoreRule.setIsDelete(1);
        matchScoreRule.setUpdateTime(new Date());
        matchScoreRule.setId(ruleId);
        matchScoreRuleService.updateById(matchScoreRule);
        return ResultUtils.ok();
    }

}
